package com.ymatou.userservice.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userservice.parameter.AliUserRequestDtoBean;
import com.ymatou.userservice.service.AliUserRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.model.YmtUsers;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.database.sqlwapper.LoginHistoryWapper;
import com.ymttest.database.sqlwapper.YmtUsersWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.AppTypeChannel;
import com.ymttest.utils.constant.ECode;
import com.ymttest.utils.constant.ThirdPartyType;
/**
*创建阿里用户接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_AliUserRequestDto{
	private static AliUserRequestDtoBean aliuserregbean;
	private static AliUserRequestDtoCall aliuserregCall;
	private static YmtUsersWapper userWapper; 
	private static UserTokenWapper userTokenWapper;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建阿里用户接口");
	}

	@Before
	public void caseUp() throws Exception {
		aliuserregbean=new AliUserRequestDtoBean();
		aliuserregCall=new AliUserRequestDtoCall();
		userWapper = new YmtUsersWapper();
		userTokenWapper = new UserTokenWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(aliuserregCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AliUserRequestDto_001() {
		Logger.start(true,"阿里账号第一次授权,创建用户成功");
		try {
			String aliUserId = "ali_"+TestDataManager.getRandomNum(10);
			Logger.comment("阿里账号第一次授权，检查需要绑定手机号");
			UserCallService.checkThirdPartyNeedBindMobile(aliUserId, ThirdPartyType.ALI, ECode.USER_YES);
			
			String phone = TestDataManager.getPhoneNum();
			Logger.comment("验证手机号是否绑定过阿里账号");
			UserCallService.checkMobileIsBindThirdParty(phone, aliUserId, ThirdPartyType.ALI, ECode.USER_NO);
			
			Logger.comment("绑定手机号，注册阿里账号");
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSRealName("regAliUser_"+TestDataManager.getRandomNum(6));
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("ios");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregbean.setMobile(phone);
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
			aliuserregbean.setDeviceId(TestDataManager.getRandomUUID().toString());
			aliuserregbean.setEquimentid(TestDataManager.getRandomUUID().toString());
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			
			Logger.verifyEquals("注册成功", aliuserregCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", aliuserregCall.getString("Success"), "验证是否成功");
			
			Logger.comment("和数据库进行数据校验");
			Thread.sleep(1000);
			Map user = userWapper.selectUserAtAliByAliId(aliUserId);
			int userId = (Integer)user.get("iYmtUserId");
			Map userToken = userTokenWapper.getUserTokenByUid(userId);
			Logger.verifyEquals(userToken.get("uId"), ((Double)aliuserregCall.getResult().get("UserId")).intValue(), "验证用户ID");
			Logger.verifyEquals(userToken.get("sId"), aliuserregCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyEquals("new", aliuserregCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals("Success", aliuserregCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
			
			Thread.sleep(1000);
			Map m=new HashMap<>();
			m.put("Uid", userId);
			Map history=new LoginHistoryWapper().selectUserRegHistoryByUid(m).get(0);
			Logger.debug(history);
			Logger.verifyEquals(aliuserregbean.getDeviceId(), history.get("DeviceId"), "验证凭据DeviceId");
			Logger.verifyEquals(aliuserregbean.getSRealName().toLowerCase(), history.get("LoginId").toString(), "验证凭据LoginId");
			Logger.verifyEquals(aliuserregbean.getMobile(), history.get("Mobile"), "验证凭据Mobile");
			Logger.verifyEquals(aliuserregbean.getClientIp(), history.get("ClientIp"), "验证凭据ClientIp");
			Logger.verifyEquals("Alipay", history.get("ChannelSource"), "验证凭据ChannelSource");
			//1 App
			Logger.verifyEquals("App", history.get("RegSource"), "验证凭据RegSource");
			Logger.verifyEquals(aliuserregbean.getEquimentid(), history.get("Equimentid"), "验证凭据Equimentid");
			YmtUsers user1 = new YmtUsersWapper().getYmtUsers(userId);
			//验证cellphone
			Logger.verifyEquals(phone, user1.getScellphone(), "验证cellphone");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AliUserRequestDto_002() {
		Logger.start(true,"用户已存在（非第一次授权），则用户直接登录成功");
		try {
			Map thirdParty = userWapper.selectOneThirdPartyBindPhone("iYmtUserId", "sUserId", "Ymt_User_At_Alipay");
			String aliUserId = thirdParty.get("sUserId").toString();
			UserCallService.checkThirdPartyNeedBindMobile(aliUserId, ThirdPartyType.ALI, ECode.USER_NO);
			
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSRealName("Lily"+TestDataManager.getRandomNum(10));
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("ios");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			
			Logger.verifyEquals("登录成功", aliuserregCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", aliuserregCall.getString("Success"), "验证是否成功");
			Thread.sleep(1000);
			Map user = userWapper.selectUserAtAliByAliId(thirdParty.get("sUserId").toString());
			int userId = (Integer)user.get("iYmtUserId");
			Map userToken = userTokenWapper.getUserTokenByUid(userId);
			
			Logger.verifyEquals((Integer)userToken.get("uId"), ((Double)aliuserregCall.getResult().get("UserId")).intValue(), "验证用户ID");
			Logger.verifyEquals(userToken.get("sId"), aliuserregCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyIsNull(aliuserregCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals("Success", aliuserregCall.getResult().get("VerifyCode"), "验证凭据");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AliUserRequestDto_003() {
		Logger.start(false,"手机号已绑定过其他阿里账号，再用该手机注册，失败");
		try {
			String phone = TestDataManager.getPhoneNum();
			String oldAliUser = "ali_"+TestDataManager.getRandomNum(10);
			UserCallService.regByAli(oldAliUser, phone, true, "注册成功");
			
			String aliUserId = "ali_"+TestDataManager.getRandomNum(10);
			Logger.comment("阿里账号第一次授权，检查需要绑定手机号");
			UserCallService.checkThirdPartyNeedBindMobile(aliUserId, ThirdPartyType.ALI, ECode.USER_YES);
			
			Logger.comment("验证手机号绑定过阿里账号");
			UserCallService.checkMobileIsBindThirdParty(phone, aliUserId, ThirdPartyType.ALI, ECode.USER_YES);
			
			Logger.comment("继续使用该手机号，注册阿里账号");
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSRealName("regAliUser_"+TestDataManager.getRandomNum(6));
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("ios");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregbean.setMobile(phone);
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			
			Logger.verifyEquals("false", aliuserregCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals(ECode.ERROR400, aliuserregCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("该手机号码已经绑定过支付宝账号了，换一个试试吧~", aliuserregCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("AlreadyUnionMatouUser", aliuserregCall.getResult().get("VerifyCode"), "验证verifyCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AliUserRequestDto_004() {
		Logger.start(false,"手机号已经注册过码头账号，第三方账号也绑定过码头账号，绑定失败");
		try {
			Logger.comment("手机注册洋码头账号");
			String phone = TestDataManager.getPhoneNum();
			Map user = UserCallService.registerUser(null, phone,null);
			
			Logger.comment("检查手机绑定了码头账号");
			UserCallService.checkMobileIsBindYmtUser(phone, ECode.USER_YES);
			
			Logger.comment("阿里账号也绑定过其他码头账号");
			String aliUserId = "ali_"+TestDataManager.getRandomNum(10);
			UserCallService.regByAli(aliUserId, TestDataManager.getPhoneNum(), true, "注册成功");
			
			Logger.comment("继续使用该手机号，注册阿里账号");
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSRealName("regAliUser_"+TestDataManager.getRandomNum(6));
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("ios");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregbean.setMobile(phone);
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			

			Logger.verifyEquals("当前账号已绑定", aliuserregCall.getString("LastErrorMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.ERROR400, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("false", aliuserregCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("AlreadyUnionMatouUser", aliuserregCall.getResult().get("VerifyCode"), "验证凭据");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_AliUserRequestDto_005() {
		Logger.start(false,"不输入Ali用户Id，用户创建失败");
		try {
			aliuserregbean.setSRealName("Lily");
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("ios");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			
			Logger.verifyEquals("false", aliuserregCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals(ECode.ERROR400, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("阿里Id不能为空", aliuserregCall.getString("LastErrorMessage"), "验证返回的Message信息");
			Logger.verifyEquals(-1d, aliuserregCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyIsNull(aliuserregCall.getResult().get("Token"), "验证Token");
			Logger.verifyIsNull(aliuserregCall.getResult().get("Remark"), "验证remark信息");
			Logger.verifyEquals("AliUserIdIsEmpty", aliuserregCall.getResult().get("VerifyCode"), "验证凭据");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	//注册时sname必传
	public void Tc_AliUserRequestDto_006(){
		Logger.start(true,"不输入真实用户名，用户创建失败");
		try {
			String aliUserId = TestDataManager.getRandomNum(7);
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSEmail(TestDataManager.getRandomNum(7)+"@Ali.com");
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("android");
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregbean.setIsNotReturnToken(true);
			aliuserregbean.setMobile(TestDataManager.getPhoneNum());
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();	
			
//			Logger.verifyEquals("注册成功", aliuserregCall.getString("SuccessMessage"), "验证返回的Message信息");
//			Logger.verifyEquals(ECode.SUCCESS, aliuserregCall.getString("ResponseCode"), "验证code");
//			Logger.verifyEquals("true", aliuserregCall.getString("Success"), "验证是否成功");
//			
//			Thread.sleep(1000);
//			Map user = userWapper.selectUserAtAliByAliId(aliUserId);
//			int userId = (Integer)user.get("iYmtUserId");
//			
//			Logger.verifyEquals(userId, ((Double)aliuserregCall.getResult().get("UserId")).intValue(), "验证用户ID");
//			Logger.verifyIsNull(aliuserregCall.getResult().get("Token"), "验证Token信息");
//			Logger.verifyEquals("new", aliuserregCall.getResult().get("Remark"), "验证Remark信息");
//			Logger.verifyEquals("Success", aliuserregCall.getResult().get("VerifyCode"), "验证凭据");
			
			Logger.verifyEquals("登录ID不能为空", aliuserregCall.getString("LastErrorMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.ERROR400, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("false", aliuserregCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("LoginNameEmpty", aliuserregCall.getResult().get("VerifyCode"), "验证凭据");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_AliUserRequestDto_007(){
		Logger.start(false,"不输入手机，用户创建失败");
		try {
			String aliUserId = TestDataManager.getRandomNum(7);
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("ios");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();	
			
			Logger.verifyEquals("为了您的账户安全，请绑定你的手机号码", aliuserregCall.getString("LastErrorMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.ERROR400, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("false", aliuserregCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("NeedBindMobile", aliuserregCall.getResult().get("VerifyCode"), "验证凭证");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_AliUserRequestDto_008() {
		Logger.start(true,"用户绑定了A阿里账号->解绑A阿里账号->绑定B阿里账号(手机号与A相同)->绑定成功");
		try {
			String phone = TestDataManager.getPhoneNum();
			Logger.comment("注册码头账号");
			UserCallService.registerUser(null, phone,null);
			
			Logger.comment("绑定了A阿里账号");
			String aliA = "unbindali_"+TestDataManager.getRandomNum(10);
			Map user = UserCallService.regByAli(aliA, phone, true, "登录成功");
			
			String aliB = "unbindali_"+TestDataManager.getRandomNum(10);
			Logger.comment("绑定B阿里账号(手机号与A相同)");
			UserCallService.regByAli(aliB, phone, false, "该手机号码已经绑定过支付宝账号了，换一个试试吧~");
			
			int userId = ((Double)user.get("userId")).intValue();
			Logger.comment("解绑A阿里账号");
			Map result = UserCallService.unbindThirdParty(userId, aliA, ThirdPartyType.ALI);
			Logger.verifyEquals(200d, result.get("ResponseCode"), "验证Code");
			Logger.verifyEquals(true, result.get("Success"), "验证是否成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证Msg信息");
			
			Logger.comment("再次绑定B阿里账号(手机号与A相同)");
			UserCallService.regByAli(aliB, phone, true, "登录成功");
			
			Logger.comment("验证用户的绑定情况");
			List<Map> thirdParty = new ArrayList<Map>();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, false);
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliB, true);
			UserCallService.checkThirdPartyBindYmt(userId, thirdParty);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_AliUserRequestDto_009() {
		Logger.start(true,"用户绑定了A阿里账号->解绑A阿里账号->用B阿里账号登录(微信号与A相同，手机号与A不同，手机未绑定过码头账号)->注册成功");
		try {
			String phone = TestDataManager.getPhoneNum();
			Logger.comment("注册码头账号");
			UserCallService.registerUser(null, phone,null);
			
			Logger.comment("绑定了A阿里账号");
			String aliA = "unbindali_"+TestDataManager.getRandomNum(10);
			Map user = UserCallService.regByAli(aliA, phone, true, "登录成功");
			int userId = ((Double)user.get("userId")).intValue();
			List<Map> thirdParty = new ArrayList<Map>();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, true);
			UserCallService.checkThirdPartyBindYmt(userId, thirdParty);
			
			Logger.comment("解绑A阿里账号");
			Map result = UserCallService.unbindThirdParty(userId, aliA, ThirdPartyType.ALI);
			Logger.verifyEquals(200d, result.get("ResponseCode"), "验证Code");
			Logger.verifyEquals(true, result.get("Success"), "验证是否成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证Msg信息");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, false);
			UserCallService.checkThirdPartyBindYmt(userId, thirdParty);
			
			Logger.comment("绑定B阿里账号(微信号与A相同，手机号与A不同)");
			String phoneB = TestDataManager.getPhoneNum();
			UserCallService.checkThirdPartyNeedBindMobile(aliA, ThirdPartyType.ALI, ECode.USER_YES);
			UserCallService.checkMobileIsBindThirdParty(phoneB, aliA, ThirdPartyType.ALI, ECode.USER_NO);
			Map bindUser = UserCallService.regByAli(aliA, phoneB, true, "注册成功");
			int newUserId = ((Double)bindUser.get("userId")).intValue();
			Logger.verifyNotEquals(userId, newUserId, "验证创建了一个新用户");
			
			Logger.comment("验证新用户的绑定情况");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, true);
			UserCallService.checkThirdPartyBindYmt(newUserId, thirdParty);
			
			Logger.comment("验证老用户的绑定情况");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, false);
			UserCallService.checkThirdPartyBindYmt(userId, thirdParty);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_AliUserRequestDto_010() {
		Logger.start(true,"用户绑定了A阿里账号->解绑A阿里账号->绑定A阿里账号->绑定成功");
		try {
			String phone = TestDataManager.getPhoneNum();
			Logger.comment("注册码头账号");
			UserCallService.registerUser(null, phone,null);
			
			Logger.comment("绑定了A阿里账号");
			String aliA = "unbindali_"+TestDataManager.getRandomNum(10);
			Map user = UserCallService.regByAli(aliA, phone, true, "登录成功");
			int userId = ((Double)user.get("userId")).intValue();
			List<Map> thirdParty = new ArrayList<Map>();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, true);
			UserCallService.checkThirdPartyBindYmt(userId, thirdParty);
			
			Logger.comment("解绑A阿里账号");
			Map result = UserCallService.unbindThirdParty(userId, aliA, ThirdPartyType.ALI);
			Logger.verifyEquals(200d, result.get("ResponseCode"), "验证Code");
			Logger.verifyEquals(true, result.get("Success"), "验证是否成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证Msg信息");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, false);
			UserCallService.checkThirdPartyBindYmt(userId, thirdParty);
			
			Logger.comment("绑定A阿里账号");
			UserCallService.checkThirdPartyNeedBindMobile(aliA, ThirdPartyType.ALI, ECode.USER_YES);
			UserCallService.checkMobileIsBindThirdParty(phone, aliA, ThirdPartyType.ALI, ECode.USER_NO);
			Map bindUser = UserCallService.regByAli(aliA, phone, true, "登录成功");
			int newUserId = ((Double)bindUser.get("userId")).intValue();
			Logger.verifyEquals(userId, newUserId, "验证绑定前后是同一个用户");
			Logger.comment("验证用户的绑定情况");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, true);
			UserCallService.checkThirdPartyBindYmt(newUserId, thirdParty);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_AliUserRequestDto_011() {
		Logger.start(true,"用户绑定了A阿里账号->解绑A阿里账号->用B阿里账号(微信号与A相同，手机号与A不同，已绑定过码头账号)->绑定成功");
		try {
			String phone = TestDataManager.getPhoneNum();
			Logger.comment("注册码头账号");
			UserCallService.registerUser(null, phone,null);
			
			Logger.comment("绑定了A阿里账号");
			String aliA = "unbindali_"+TestDataManager.getRandomNum(10);
			Map user = UserCallService.regByAli(aliA, phone, true, "登录成功");
			int userId = ((Double)user.get("userId")).intValue();
			List<Map> thirdParty = new ArrayList<Map>();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, true);
			UserCallService.checkThirdPartyBindYmt(userId, thirdParty);
			
			Logger.comment("解绑A阿里账号");
			Map result = UserCallService.unbindThirdParty(userId, aliA, ThirdPartyType.ALI);
			Logger.verifyEquals(200d, result.get("ResponseCode"), "验证Code");
			Logger.verifyEquals(true, result.get("Success"), "验证是否成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证Msg信息");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, false);
			UserCallService.checkThirdPartyBindYmt(userId, thirdParty);
			
			Logger.comment("绑定B阿里账号(微信号与A相同，手机号与A不同，已绑定过码头账号)");
			String phoneB = TestDataManager.getPhoneNum();
			Map userB = UserCallService.registerUser(null, phoneB,null);
			Logger.verifyNotEquals(-1, (Integer)userB.get("userId"), "验证创建成功");
			
			UserCallService.checkThirdPartyNeedBindMobile(aliA, ThirdPartyType.ALI, ECode.USER_YES);
			UserCallService.checkMobileIsBindThirdParty(phoneB, aliA, ThirdPartyType.ALI, ECode.USER_NO);
			UserCallService.checkMobileIsBindYmtUser(phoneB, "yes");
			Map bindUser = UserCallService.regByAli(aliA, phoneB, true, "登录成功");
			int newUserId = ((Double)bindUser.get("userId")).intValue();
			Logger.verifyEquals((Integer)userB.get("userId"), newUserId, "验证创建了一个新用户");
			
			Logger.comment("验证新用户的绑定情况");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, true);
			UserCallService.checkThirdPartyBindYmt(newUserId, thirdParty);
			
			Logger.comment("验证老用户的绑定情况");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, false);
			UserCallService.checkThirdPartyBindYmt(userId, thirdParty);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_AliUserRequestDto_012() {
		Logger.start(true,"第三方用户已存在（非第一次授权）,绑定不同的手机号，则用户直接登录成功");
		try {
			Map thirdParty = userWapper.selectOneThirdPartyBindPhone("iYmtUserId", "sUserId", "Ymt_User_At_Alipay");
			String aliUserId = thirdParty.get("sUserId").toString();
			UserCallService.checkThirdPartyNeedBindMobile(aliUserId, ThirdPartyType.ALI, ECode.USER_NO);
			
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSRealName("Lily"+TestDataManager.getRandomNum(10));
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("ios");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregbean.setMobile(TestDataManager.getPhoneNum());
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			
			Logger.verifyEquals("当前账号已绑定", aliuserregCall.getString("LastErrorMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.ERROR400, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("false", aliuserregCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("AlreadyUnionMatouUser", aliuserregCall.getResult().get("VerifyCode"), "验证凭据");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AliUserRequestDto_013() {
		Logger.start(true,"手机号已经注册过码头账号，选择继续绑定，绑定成功");
		try {
			Logger.comment("手机注册洋码头账号");
			String phone = TestDataManager.getPhoneNum();
			Map user = UserCallService.registerUser(null, phone,null);
			
			Logger.comment("检查手机绑定了码头账号");
			UserCallService.checkMobileIsBindYmtUser(phone, ECode.USER_YES);
			
			String aliUserId = "ali_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证手机号是否绑定过阿里账号");
			UserCallService.checkMobileIsBindThirdParty(phone, aliUserId, ThirdPartyType.ALI, ECode.USER_NO);
			
			Logger.comment("继续使用该手机号，注册阿里账号");
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSRealName("regAliUser_"+TestDataManager.getRandomNum(6));
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("ios");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregbean.setMobile(phone);
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			

			Logger.verifyEquals("登录成功", aliuserregCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", aliuserregCall.getString("Success"), "验证是否成功");
			
			Logger.comment("检查该用户绑定了阿里账号");
			List<Map> thirdParty = new ArrayList<Map>();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliUserId, true);
			UserCallService.checkThirdPartyBindYmt((Integer)user.get("userId"), thirdParty);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_AliUserRequestDto_014() {
		Logger.start(true,"阿里账号第一次授权,创建用户成功,用户名长度大于20");
		try {
			String aliUserId = "ali_"+TestDataManager.getRandomNum(10);
			Logger.comment("阿里账号第一次授权，检查需要绑定手机号");
			UserCallService.checkThirdPartyNeedBindMobile(aliUserId, ThirdPartyType.ALI, ECode.USER_YES);
			
			String phone = TestDataManager.getPhoneNum();
			Logger.comment("验证手机号是否绑定过阿里账号");
			UserCallService.checkMobileIsBindThirdParty(phone, aliUserId, ThirdPartyType.ALI, ECode.USER_NO);
			
			Logger.comment("绑定手机号，注册阿里账号");
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSRealName("regAliUser_"+TestDataManager.getRandomNum(20));
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("ios");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregbean.setMobile(phone);
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			
			Logger.verifyEquals("注册成功", aliuserregCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", aliuserregCall.getString("Success"), "验证是否成功");
			
			Logger.comment("和数据库进行数据校验");
			Thread.sleep(1000);
			Map user = userWapper.selectUserAtAliByAliId(aliUserId);
			int userId = (Integer)user.get("iYmtUserId");
			Map userToken = userTokenWapper.getUserTokenByUid(userId);
			Logger.verifyEquals(userToken.get("uId"), ((Double)aliuserregCall.getResult().get("UserId")).intValue(), "验证用户ID");
			Logger.verifyEquals(userToken.get("sId"), aliuserregCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyEquals("new", aliuserregCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals("Success", aliuserregCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_AliUserRequestDto_015() {
		Logger.start(false,"阿里账号第一次授权,创建用户失败,用户名长度大于100");
		try {
			String aliUserId = "ali_"+TestDataManager.getRandomNum(10);
			Logger.comment("阿里账号第一次授权，检查需要绑定手机号");
			UserCallService.checkThirdPartyNeedBindMobile(aliUserId, ThirdPartyType.ALI, ECode.USER_YES);
			
			String phone = TestDataManager.getPhoneNum();
			Logger.comment("验证手机号是否绑定过阿里账号");
			UserCallService.checkMobileIsBindThirdParty(phone, aliUserId, ThirdPartyType.ALI, ECode.USER_NO);
			
			Logger.comment("绑定手机号，注册阿里账号");
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSRealName("regAliUser_"+TestDataManager.getRandomNum(100));
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("ios");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregbean.setMobile(phone);
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			
			String VerifyCode=aliuserregCall.getJsonObject().get("Result").getAsJsonObject().get("VerifyCode").getAsString();
			Logger.verifyEquals("LoginNameLengthError", VerifyCode, "验证返回的LoginNameLengthError信息");
			Logger.verifyEquals("400", aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("false", aliuserregCall.getString("Success"), "验证是否成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AliUserRequestDto_016() {
		Logger.start(true,"传入TokenSource为sellerapp用户注册成功JIRACONN-2123");
		try {
			String aliUserId = "ali_"+TestDataManager.getRandomNum(10);
			Logger.comment("阿里账号第一次授权，检查需要绑定手机号");
			UserCallService.checkThirdPartyNeedBindMobile(aliUserId, ThirdPartyType.ALI, ECode.USER_YES);
			
			String phone = TestDataManager.getPhoneNum();
			Logger.comment("验证手机号是否绑定过阿里账号");
			UserCallService.checkMobileIsBindThirdParty(phone, aliUserId, ThirdPartyType.ALI, ECode.USER_NO);
			
			Logger.comment("绑定手机号，注册阿里账号");
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSRealName("regAliUser_"+TestDataManager.getRandomNum(6));
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("ios");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregbean.setMobile(phone);
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
			aliuserregbean.setDeviceId(TestDataManager.getRandomUUID().toString());
			aliuserregbean.setEquimentid(TestDataManager.getRandomUUID().toString());
			aliuserregbean.setTokenSource("sellerapp");
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			
			Logger.verifyEquals("注册成功", aliuserregCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", aliuserregCall.getString("Success"), "验证是否成功");
			
			Logger.comment("和数据库进行数据校验");
			Thread.sleep(1000);
			Map user = userWapper.selectUserAtAliByAliId(aliUserId);
			int userId = (Integer)user.get("iYmtUserId");
			Map userToken = userTokenWapper.getUserTokenByUid(userId);
			Logger.verifyEquals(userToken.get("uId"), ((Double)aliuserregCall.getResult().get("UserId")).intValue(), "验证用户ID");
			Logger.verifyEquals(userToken.get("sId"), aliuserregCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyEquals("new", aliuserregCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals("Success", aliuserregCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
			
			Thread.sleep(1000);
			Map m=new HashMap<>();
			m.put("Uid", userId);
			Map history=new LoginHistoryWapper().selectUserRegHistoryByUid(m).get(0);
			Logger.debug(history);
			Logger.verifyEquals(aliuserregbean.getDeviceId(), history.get("DeviceId"), "验证凭据DeviceId");
			Logger.verifyEquals(aliuserregbean.getSRealName().toLowerCase(), history.get("LoginId").toString(), "验证凭据LoginId");
			Logger.verifyEquals(aliuserregbean.getMobile(), history.get("Mobile"), "验证凭据Mobile");
			Logger.verifyEquals(aliuserregbean.getClientIp(), history.get("ClientIp"), "验证凭据ClientIp");
			Logger.verifyEquals("Alipay", history.get("ChannelSource"), "验证凭据ChannelSource");
			//1 App
			Logger.verifyEquals("App", history.get("RegSource"), "验证凭据RegSource");
			Logger.verifyEquals(aliuserregbean.getEquimentid(), history.get("Equimentid"), "验证凭据Equimentid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AliUserRequestDto_017() {
		Logger.start(true,"传入TokenSource为app用户注册成功JIRACONN-2123");
		try {
			String aliUserId = "ali_"+TestDataManager.getRandomNum(10);
			Logger.comment("阿里账号第一次授权，检查需要绑定手机号");
			UserCallService.checkThirdPartyNeedBindMobile(aliUserId, ThirdPartyType.ALI, ECode.USER_YES);
			
			String phone = TestDataManager.getPhoneNum();
			Logger.comment("验证手机号是否绑定过阿里账号");
			UserCallService.checkMobileIsBindThirdParty(phone, aliUserId, ThirdPartyType.ALI, ECode.USER_NO);
			
			Logger.comment("绑定手机号，注册阿里账号");
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSRealName("regAliUser_"+TestDataManager.getRandomNum(6));
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("ios");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregbean.setMobile(phone);
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
			aliuserregbean.setDeviceId(TestDataManager.getRandomUUID().toString());
			aliuserregbean.setEquimentid(TestDataManager.getRandomUUID().toString());
			aliuserregbean.setTokenSource("app");
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			
			Logger.verifyEquals("注册成功", aliuserregCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", aliuserregCall.getString("Success"), "验证是否成功");
			
			Logger.comment("和数据库进行数据校验");
			Thread.sleep(1000);
			Map user = userWapper.selectUserAtAliByAliId(aliUserId);
			int userId = (Integer)user.get("iYmtUserId");
			Map userToken = userTokenWapper.getUserTokenByUid(userId);
			Logger.verifyEquals(userToken.get("uId"), ((Double)aliuserregCall.getResult().get("UserId")).intValue(), "验证用户ID");
			Logger.verifyEquals(userToken.get("sId"), aliuserregCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyEquals("new", aliuserregCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals("Success", aliuserregCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
			
			Thread.sleep(1000);
			Map m=new HashMap<>();
			m.put("Uid", userId);
			Map history=new LoginHistoryWapper().selectUserRegHistoryByUid(m).get(0);
			Logger.debug(history);
			Logger.verifyEquals(aliuserregbean.getDeviceId(), history.get("DeviceId"), "验证凭据DeviceId");
			Logger.verifyEquals(aliuserregbean.getSRealName().toLowerCase(), history.get("LoginId").toString(), "验证凭据LoginId");
			Logger.verifyEquals(aliuserregbean.getMobile(), history.get("Mobile"), "验证凭据Mobile");
			Logger.verifyEquals(aliuserregbean.getClientIp(), history.get("ClientIp"), "验证凭据ClientIp");
			Logger.verifyEquals("Alipay", history.get("ChannelSource"), "验证凭据ChannelSource");
			//1 App
			Logger.verifyEquals("App", history.get("RegSource"), "验证凭据RegSource");
			Logger.verifyEquals(aliuserregbean.getEquimentid(), history.get("Equimentid"), "验证凭据Equimentid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AliUserRequestDto_018() {
		Logger.start(false,"特殊字符用户名失败JIRACONN-2522");
		try {
			String aliUserId = "ali_"+TestDataManager.getRandomNum(10);
			
			String phone = TestDataManager.getPhoneNum();
			
			Logger.comment("绑定手机号，注册阿里账号");
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSRealName("~!@#$%^&*");
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("ios");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
			aliuserregbean.setMobile(phone);
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
			aliuserregbean.setDeviceId(TestDataManager.getRandomUUID().toString());
			aliuserregbean.setEquimentid(TestDataManager.getRandomUUID().toString());
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			
			String VerifyCode=aliuserregCall.getJsonObject().get("Result").getAsJsonObject().get("VerifyCode").getAsString();
			Logger.verifyEquals("LoginNameContextError", VerifyCode, "验证返回的LoginNameContextError信息");
			Logger.verifyEquals(ECode.ERROR400, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("false", aliuserregCall.getString("Success"), "验证是否成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AliUserRequestDto_019() {
		Logger.start(false,"用户服务HotFix支付宝用户不传手机绑定成功,JIRACONN-7365");
		try {
			String aliUserId = "ali_"+TestDataManager.getRandomNum(10);
			
			Logger.comment("绑定手机号，注册阿里账号");
			aliuserregbean.setSAliUserId(aliUserId);
			aliuserregbean.setSRealName("regAliUser_"+TestDataManager.getRandomNum(6));
			aliuserregbean.setSEmail("12456@123.com");
			aliuserregbean.setRegSource(1);
			aliuserregbean.setTerminalSource("ios");
			aliuserregbean.setClientIp("120.1.1.1");
			aliuserregbean.setTokenTimeOut((new Date()).getTime());
//			aliuserregbean.setMobile(phone);
			aliuserregbean.setAppTypeChanndel(AppTypeChannel.OLD_CHANNEL);
			aliuserregbean.setDeviceId(TestDataManager.getRandomUUID().toString());
			aliuserregbean.setEquimentid(TestDataManager.getRandomUUID().toString());
			aliuserregCall.setData(aliuserregbean);
			aliuserregCall.callService();			
			Logger.verifyIsContains("绑定", aliuserregCall.getString("LastErrorMessage"), "验证返回的LastErrorMessage信息");
			Logger.verifyEquals(ECode.ERROR400, aliuserregCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("false", aliuserregCall.getString("Success"), "验证是否成功");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}